import Vue from 'vue'
import Vuex from 'vuex'
// import { SetItem, GetItem } from "@/utils/localStorage.js";
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
// const token = 'UserToken';
import { getHouse } from '@/api'
export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // user: GetItem("UserToken") ? GetItem("UserToken") : null,
    user: null, // 使用持久化插件不需要存本地了
    city: '深圳',
    cityValue: null,
    houseList: [],
    houseId: null,
    communityS: null,
    communitySName: null
  },
  getters: {},
  mutations: {
    setToken(state, token) {
      state.user = token
    },
    loginOutSetToken(state, token) {
      state.user = null
    },
    setCity(state, city) {
      state.city = city
    },
    setCityValue(state, value) {
      state.cityValue = value
    },
    SET_HOUSELIST(state, value) {
      state.houseList = value
    },
    // 下拉添加数据
    ADD_HOUSELIST(state, value) {
      state.houseList.push(...value)
    },
    // 存储房屋详细id
    SET_HOUSEID(state, value) {
      state.houseId = value
    },
    // 查询小区名称
    SET_COMMUNITY(state, { community, communityName }) {
      state.communityS = community
      state.communitySName = communityName
    }
  },
  actions: {
    async getNewHouseList({ state, commit }, val = null) {
      const { data: res } = await getHouse({
        city: state.cityValue,
        rentType: val
      })
      commit('SET_HOUSELIST', res.body.list)
    }
  },
  modules: {}
})
